package de.hbrs.ooka.crs.servlet;

import de.hbrs.ooka.beans.ConferenceBeanStatelessLocal;
import de.hbrs.ooka.beans.PaperBeanStatelessLocal;
import de.hbrs.ooka.beans.UserBeanStatelessLocal;
import de.hbrs.ooka.beans.UserSessionStatefulLocal;
import de.hbrs.ooka.crs.servlet.constants.WebPageNames;
import de.hbrs.ooka.entities.UserEntity;
import de.hbrs.ooka.entities.dtos.ConferenceTransferObject;
import de.hbrs.ooka.enums.Role;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OrganizerViewServlet extends AbstractViewServlet {

    @EJB
    private PaperBeanStatelessLocal paperBean;
    @EJB
    private ConferenceBeanStatelessLocal confBean;
    @EJB
    private UserBeanStatelessLocal userBean;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        UserSessionStatefulLocal crsBean = this.getCrsBean(request);

        double reputation = this.userBean.getReputation(crsBean.getUser());
        
        String redirection = "";
        RequestDispatcher rd = null;

        if (crsBean == null || !this.isUserAuthenticated(crsBean.getUser(), Role.ORGANIZER)) {
            request.setAttribute("message", "Not logged in");
            request.setAttribute("redirect", WebPageNames.LoginViewPage);
            rd = request.getRequestDispatcher("/" + WebPageNames.ErrorPage + ".jsp");
            if (rd != null) {
                rd.forward(request, response);
            }
        } else {

            String action = request.getParameter("action");
            List<UserEntity> users;

            if (action == null) {
                request.setAttribute("name", crsBean.getUser().getName());
                request.setAttribute("roles", crsBean.getUser().getRoles());
                request.setAttribute("reputation", reputation);
                
                List<ConferenceTransferObject> myConfs = new ArrayList<ConferenceTransferObject>();
                for (ConferenceTransferObject conferenceEntity : this.confBean.getAllConferences()) {
                    if (crsBean.getUser().equals(conferenceEntity.getOrganizer())) {
                        myConfs.add(conferenceEntity);
                    }
                }

                request.setAttribute("myConferenceList", myConfs);
                rd = request.getRequestDispatcher("/" + WebPageNames.OrganizerViewPage + ".jsp");
                if (rd != null) {
                    rd.forward(request, response);
                }
            } else if (action.equalsIgnoreCase("getroles")) {
                int userId = Integer.parseInt(request.getParameter("user"));

                UserEntity user = this.userBean.getUser(userId);
                List<Role> roles = null;
                String roleString = "[";
                if (user != null) {
                    roles = user.getRoles();
                    for (Role role : roles) {
                        roleString += "\"" + role.toString().toLowerCase() + "\",";
                    }
                }
                roleString = roleString.substring(0, roleString.length() - 1);
                roleString += "]";
                response.setContentType("application/json");
                response.setCharacterEncoding("UTF-8");
                Writer wr = response.getWriter();
                wr.write(roleString);
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}